import list from './components/list.vue'
import retailList from './components/retailList.vue'
export default {
  data() {
    return {
      orderList: [],
      retailOrderList: [],
      list: [{
        name: '购车订单(询价)'
      },
      {
        name: '卖车订单(报价)'
      },
      {
        name: '卖车订单(零售)'
      }
      ],
      sellPage: {
        index: 1,
        size: 10
      }, //报价
      buyPage: {
        index: 1,
        size: 10
      }, //询价
      retailPage: {
        index: 1,
        size: 10
      },
      current: 0,
      swiperCurrent: 0,
      tabsHeight: 0,
      dx: 0,
      dealId: '',
      loadStatus: ['loadmore', 'loading', 'nomore'],
      loadStatusId: 0,
      inquiryStatusTitleList: ['全部', '待付款', '待确认', '待发货', '待收货', '已完成'],
      inquiryCurrent: 0, //询价
      quotation: 0, //报价
      retailCurrent: 0,
      contractStatus: '', //订单状态
      popup: {
        show: false,
        title: '',
        btnText: '',
        mode: 'middle'
      },
      totalpage: 0,
      identity: '',
      retail: 0, //零售选项
      redetail: 0, //二层选项
      // 第一层tab切换订单类型
      RetailList: [{
        name: '预约'
      }, {
        name: '1成首付/以租代售'
      },
      /* {
        name: ''
      }, */
      {
        name: '寻车单'
      }],
      RetailListNoFindCar: [{
        name: '预约'
      }, {
        name: '1成首付/以租代售'
      },
        /* {
        name: ''
      }, */
      ], //无寻车单
      // 第二层tab切换订单状态
      RetailStatusList: [
        ['全部', '待到店', '已到店', '未成交', '已成交'],
        ['全部', '待审核', '待补资料', '未通过', '待付款', '待交车'],
        // ['全部', '待审核', '待补资料', '未通过', '待付款', '待交车'],
        ['全部', '待报价', '待确认', '已完成']
      ],
      //一成首付状态
      LowPayList: [{
        val: -1,
        name: '全部'
      }, {
        val: 0,
        name: '待审核'
      }, {
        val: 1,
        name: '未通过'
      }, {
        val: 2,
        name: '待补资料'
      }, {
        val: 3,
        name: '待付款'
      }, {
        val: 4,
        name: '待交车'
      }],
      //寻车单
      findCarList: [{
        val: -1,
        name: '全部'
      }, {
        val: 0,
        name: '待报价'
      }, {
        val: 1,
        name: '待确认'
      }, {
        val: 2,
        name: '已完成'
      }, {
        val: 3,
        name: '关闭'
      }],
      hasFindCartype: false,
      clickChange: true,
      timeshow: false,//是否第一次加载
      canshowing: false
    }
  },

  computed: {
    hasFindCarShow: {
      get() {
        return this.hasFindCartype
      },
      set(v) {
        this.hasFindCartype = v
      }
    },

    tabLabelList() {
      let result = this.hasFindCarShow ? this.RetailList : this.RetailListNoFindCar
      return result
    }
  },

  components: {
    list,
    retailList
  },
  onLoad(options) {
    console.log(options)
    this.goInPage = options.type ? options.type : ''
    this.identity = this.$store.state.user.userInfo.permious
    this.dealId = this.$store.state.userInfoDetail.store.storeId
    this.GetStoreType()
    if (this.identity === 'Factory') {
      this.current = 1
    }
    this.getOrderList(this.current, this.contractStatus)
    console.log(this.identity, this.dealId)

  },
  onShow(){
    if(this.timeshow){
      this.orderList=[]
			 this.getOrderList(this.current, this.contractStatus)
    }

  },
  onBackPress(options) {

    if (options.from === 'backbutton') {
      if(this.goInPage='msg'){
        uni.navigateBack()
				 console.log(1)
        return true
      }else{
        uni.switchTab({
				  url: '/pages/me/me'
        })
        console.log(2)
        return true
      }

    }
  },
  methods: {
    // 该账户是否有零售订单---寻车单类型
    async GetStoreType() {
      try {
        const res = await this.$api.carOrder.GetStoreType({
          'storeid': this.dealId
        })
        this.hasFindCartype = res.data.type
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 监听按钮
    handleOrder(val) {
      console.log(val)
      if (val.val === 'del') {
        this.popup.show = true
        this.popup.title = '请确认您是否删除此订单'
        this.popup.btnText = '确认删除'
        this.popup.val = val.val
        this.popup.id = val.id
      } else if (val.val === 'getGoods') {
        this.popup.show = true
        this.popup.title = '请确认您是否已收到完好货品？'
        this.popup.btnText = '确认收货'
        this.popup.val = val.val
        this.popup.id = val.id
      }
    },
    // 滑动加载
    reachBottom() {
      console.log('滑动')
      // 此tab为空数据
      // totalpage获取数据总数
      if (this.totalpage === 0) {
        this.getOrderList(this.current, this.contractStatus)
      } else {
        let zs = Math.ceil(this.totalpage / 10) //总共多少页
        // 买车订单
        if (this.current === 0) {
          if (zs > this.buyPage.index) {
            this.buyPage.index++
            this.getOrderList(this.current, this.contractStatus)
          } else {
            this.loadStatusId = 2
          }

        } else if (this.current === 1) {
          // 卖车订单
          if (zs > this.sellPage.index) {
            this.sellPage.index++
            this.getOrderList(this.current, this.contractStatus)
          } else {
            this.loadStatusId = 2
          }
        } else {
          // 卖车订单
          if (zs > this.retailPage.index) {
            this.retailPage.index++
            this.getRetailList(this.retail, this.redetail)
          } else {
            this.loadStatusId = 2
          }
        }

      }

    },
    // 订单状态点击事件
    sectionChange(index) {
      this.retailOrderList = []
      this.orderList = []
      if (this.current === 0) {
        // 询价订单
        this.buyPage.index = 1
        this.inquiryCurrent = index
        this.contractStatus = JSON.stringify(JSON.parse(this.inquiryCurrent)) - 1
        this.contractStatus = this.contractStatus === -1 ? '' : this.contractStatus //订单状态 点击全部 状态变为空
        this.getOrderList(this.current, this.contractStatus)
      } else if (this.current === 1) {
        // 卖车订单
        this.sellPage.index = 1
        this.quotation = index
        this.contractStatus = JSON.stringify(JSON.parse(this.quotation)) - 1
        this.contractStatus = this.contractStatus === -1 ? '' : this.contractStatus //订单状态 点击全部 状态变为空
        this.getOrderList(this.current, this.contractStatus)
      } else {
        // 零售订单
        this.retailPage.index = 1
        this.retailCurrent = index
        this.getRetailList(this.retail, this.redetail)
      }

    },
    // 零售订单 //零售种类状态 订单状态
    async getRetailList(orderType, orderstatus) {
      console.log(orderType,'寻车111')
      // orderType, orderstatus（二级菜单）
      // 预约订单	状态(0、全部 1、待到店 2、已到店 3、未成交 4、已成交 )
      uni.showLoading({
        title: '加载中'
      })
      console.log(orderType, orderstatus)
      let data = {
        'pageIndex': this.retailPage.index,
        'pageSize': 10, //this.retailPage.size,
        'storeid': this.dealId,
        // 'storeid': '4d3219a6204245dca2360ce160d1dfb6',
        'status': orderstatus
      }
      // 对应订单状态值 低首付 以租代售
      if (orderType === 1) {
        this.LowPayList.forEach(it => {
          if (it.name === this.RetailStatusList[orderType][orderstatus]) {
            data.status = it.val
          }
        })
      } else if (orderType === 2) {
        this.findCarList.forEach(it => {
          if (it.name === this.RetailStatusList[orderType][orderstatus]) {
            data.status = it.val
          }
        })
      }

      let res = {}
      if (orderType === 0) {
        res = await this.$api.carOrder.GetCarBookList(data)
      } else if (orderType === 1) {
        res = await this.$api.carOrder.GetOrderBuyCarList(data)
      } else {
        res = await this.$api.carOrder.GetCarFindList(data)
      }
      try {
        uni.hideLoading()

        if (res.data.list) {
          // 处理车辆颜色
          if (orderType === 0) {
            res.data.list.forEach(it => {
              it.colour = JSON.parse(it.color)
            })
          } else if (orderType === 1 || orderType === 2 || orderType === 3) {
            res.data.list.forEach(it => {
              it.colour = JSON.parse(it.colour)
            })
          }

          // 判断是否继续加载
          this.loadStatusId = 0
          this.retailOrderList = this.retailOrderList.concat(res.data.list)
          this.clickChange = true
          this.totalpage = res.data.total
          console.log(this.retailOrderList)
          if (this.totalpage < 10) {
            this.loadStatusId = 2
          }
        } else {
          this.loadStatusId = 2
          this.retailOrderList = []
        }
      } catch (err) {
        uni.hideLoading()
        console.log(err)
      }

    },
    // 页面数据 (当前tab，当前状态)
    async getOrderList(tabType, status) {

      uni.showLoading({
        title: '加载中'
      })
      this.loadStatusId = 1
      let data = {
        'PageIndex': tabType === 0 ? this.buyPage.index : this.sellPage.index,
        'PageSize': tabType === 0 ? this.buyPage.size : this.sellPage.size,
        'contract_status': String(this.contractStatus)
      }
      if (tabType === 0) {
        data.buyer_store_id = this.dealId
      } else {
        data.seller_store_id = this.dealId
      }
      // this.orderList = []
      const res = tabType === 0 ? await this.$api.carOrder.BuyCarOrderListDataList(data) : await this.$api.carOrder.SellCarOrderListDataList(
        data)
      try {

        uni.hideLoading()
        if (res.code === 200) {
          if (res.data.list) {
            this.timeshow=true
            res.data.list.forEach(it => {
              it.car_colour = JSON.parse(it.car_colour)
            })
            this.loadStatusId = 0
            this.orderList = this.orderList.concat(res.data.list)
            this.canshowing=true
            this.clickChange = true
            this.totalpage = res.data.total
            if (this.totalpage < 10) {
              this.loadStatusId = 2
            }
          } else {
            this.loadStatusId = 2
            this.orderList = []
            this.canshowing=true
          }

        }
      } catch (err) {
        console.log(err)
      }

    },
    // 删除订单
    async DeleteOrderTask(id) {
      let _this = this
      var req = {
        'contractid': id,
        'storeid': this.dealId
      }
      const res = await this.$api.carOrder.DeleteOrderTask(req)
      try {
        // console.log('删除', res)
        this.popup.show = false
        if (res.code === 200) {
          uni.showToast({
            title: '删除成功',
            duration: 2000,
            icon: 'success'
          })
          // setTimeout(function() {
          // this.getOrderList(this.current, 0)
          this.forcUpdata()
          // }, 2000)
        }
      } catch (e) {
        //TODO handle the exception
      }
    },
    // 零售订单列表有操作时 需要刷新列表
    forcUpdata() {
      this.orderList = []
      this.retailOrderList = []
      // this.current=index
      if (this.swiperCurrent === 2) {
        // this.getRetailList(0, 0)
        this.getRetailList(this.retail, this.redetail)
      } else {
        this.getOrderList(this.swiperCurrent, 0)
      }
    },

    // 确认收货
    async confirmReceipt(id) {
      let _this = this
      var req = {
        'contractid': id,
        'storeid': this.dealId
      }
      const res = await this.$api.carOrder.OrderReceiveTask(req)
      try {
        // console.log('确认收货', res)
        if (res.code === 200) {
          this.popup.show = false
          uni.showToast({
            title: '已确认',
            duration: 2000,
            icon: 'success'
          })

          this.forcUpdata()
        }
      } catch (e) {
        //TODO handle the exception
      }
    },

    // tab栏切换
    change(index) {
      // console.log(index, '零售订单')
      this.swiperCurrent = index
      this.clickChange = false
      this.orderList = []
      if (index === 2) {
        this.getRetailList(0, 0)
      } else {
        this.getOrderList(index, 0)
      }

    },
    // swiper 改变
    orderListchange(e) {
      if (this.clickChange) {
        this.orderList = []
        this.retailOrderList = []
        console.log(e.detail.current)
        this.swiperCurrent = e.detail.current
        if (e.detail.current === 2) {
          this.getRetailList(this.retail, this.redetail)
        } else {
          this.getOrderList(e.detail.current, 0)
        }
      }

    },
    // swiper 切换事件
    transition({
      detail: {
        dx
      }
    }) {

      this.$refs.tabs.setDx(dx)
    },
    animationfinish({
      detail: {
        current
      }
    }) {
      // console.log(current)
      this.$refs.tabs.setFinishCurrent(current)
      this.swiperCurrent = current
      this.current = current
      // this.getOrderList(current,0)
    },
    // 零售订单 种类切换
    RetailChange(index) {
      this.retailOrderList = []
      this.retail = index
      this.retailPage.index = 1
      this.getRetailList(index, 0)
      console.log(index)
    },
    //// 零售订单 订单状态切换
    RetailStatusChange(index) {
      console.log('订单状态切换', index)
      this.redetail = index
      this.retailOrderList = []
      this.retailPage.index = 1
      this.getRetailList(this.retail, index)
    }
  }
}
